//app.js
App({
  onLaunch: function () {
    this.updateMini();
  },
  updateMini: function () {
    var update = wx.getUpdateManager();
    update.onCheckForUpdate(function (r) {
      if (r.hasUpdate) {
        update.onUpdateReady(function () {
          wx.showModal({
            title: '提示',
            content: '为了您更好的小程序，请您重新打开以使用新版',
            showCancel: false,
            success(res) {
              if (res.confirm) {
                update.applyUpdate();
              }
            }
          })
        });
      }
    });
  },
  getUserInfo: function (config) {
    var that = this;
    that.updateMini();
    if (this.globalData.userInfo && !config.refresh) {
      config.success && config.success(this.globalData.userInfo);
    } else {
      //调用登录接口
      wx.login({
        fail: function (res) {
          config.error && config.error(res);
        },
        success: function (res) {
          var userCode = res.code,
            openId = wx.getStorageSync('openId');
          that.req({
            url: "/wx/user/" + that.globalData.appId + "/login",
            data: {
              code: userCode,
              openId
            },
            success(res) {
              if (200 == res.code && "user" == res.result.type) {
                let data = res.result;
                let userInfo = data.data;
                userInfo.password = data.token;
                that.globalData.userInfo = userInfo;
                config.success && config.success(data.data);
              } else {
                config.error && config.error(res);
              }
            },
            error(res) {
              config.error && config.error(res);
            }
          })
        }
      })
    }
  },
  req: function (cfg) {
    var that = this;
    if (!cfg.noLoading) {
      wx.showLoading({
        title: cfg.showLoadingText || '加载中',
        mask: true
      })
    }
    if (!cfg.data) {
      cfg.data = {}
    }
    var url = cfg.purl ? cfg.purl : this.globalData.url + cfg.url;
    console.log( this.globalData.userInfo)
    wx.request({
      url: url,
      data: cfg.data,
      method: cfg.method ? cfg.method : 'GET',
      header: {
        'X-Access-Token': this.globalData.userInfo ? this.globalData.userInfo.password : ''
      },
      success: function (res) {
        if (res && res.statusCode == 200) {
          cfg.success && cfg.success(res.data);
        } else {
          that.showToast({
            icon: 'none',
            title: '请求失败',
            duration: 2000
          })
        }
      },
      fail: function (a) {
        that.showToast({
          title: '请求失败，请稍后再试',
          icon: "none",
          duration: 2000
        })
      },
      complete: function () {
        if (!cfg.noLoading) {
          wx.hideLoading();
        }
        wx.stopPullDownRefresh();
        if (cfg.complete) {
          cfg.complete();
        }
      }
    });
  },
  showToast: function (c) {
    var config = {
      title: c.title,
      icon: c.icon || 'success',
      image: c.image || '',
      duration: c.duration || 1500,
      mask: !!c.mask,
      fail: c.fail,
      complete: c.complete
    };
    if (c.success) {
      config.success = function () {
        setTimeout(c.success, config.duration);
      }
    }
    wx.showToast(config)
  },
  upload(url, callback) {
    wx.uploadFile({
      url: this.globalData.url + '/sys/common/upload', //仅为示例，非真实的接口地址
      filePath: url,
      name: 'file',
      header: {
        'X-Access-Token': this.globalData.userInfo ? this.globalData.userInfo.password : ''
      },
      success(res) {
        if (200 == res.statusCode && 0 == JSON.parse(res.data).code) {
          callback && callback(JSON.parse(res.data));
        }
      }
    })
  },
  globalData: {
    appId: "wx7ac6ed39d0d2af6d",
    userInfo: null,
    url: 'http://192.168.31.57:8080/jeecg-boot/',
    showUrl: 'http://192.168.31.57:8080/jeecg-boot/sys/common/static/',
        // url: 'https://biosune.dc-mannequins.com/jeecg-boot',
        // showUrl: 'https://biosune.dc-mannequins.com/jeecg-boot/sys/common/static/',
    statusBarHeight: wx.getSystemInfoSync()['statusBarHeight'],
  }
});